/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cliente.java
 *
 * Created on 22/04/2011, 01:17:44
 */

package tp1.views;

import javax.swing.JButton;
import javax.swing.JLabel;
import tp1.controllers.Controller;
import java.sql.SQLException;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import tp1.dao.LinhaDao;

/**
 *
 * @author tulio
 */
public class LinhaPanel extends ViewAbstract {

    /** Creates new form Cliente */
    public LinhaPanel() {
        initComponents();
        initGrid();
        initToolbar();
    }

    private void initToolbar(){
        Controller controllerLinha = Controller.factory(Controller.CONTROLLER_LINHA, this);

        controllerLinha.addEvent(buttonSalvar, Controller.EVENTO_SALVAR_LINHA);
        controllerLinha.addEvent(buttonNovo, Controller.EVENTO_NOVO_LINHA);
        controllerLinha.addEvent(buttonEditar, Controller.EVENTO_EDITAR_LINHA);
        controllerLinha.addEvent(buttonApagar, Controller.EVENTO_APAGAR_LINHA);
        controllerLinha.addEvent(buttonBuscar, Controller.EVENTO_BUSCAR_LINHA);
        controllerLinha.addEvent(buttonConta, Controller.EVENTO_RELATORIO_LINHA);

        buttonSalvar.addActionListener(controllerLinha);
        buttonNovo.addActionListener(controllerLinha);
        buttonApagar.addActionListener(controllerLinha);
        buttonEditar.addActionListener(controllerLinha);
        buttonBuscar.addActionListener(controllerLinha);
        buttonConta.addActionListener(controllerLinha);
    }

    public void updateGridData() throws SQLException, Exception{
        Object[][] data = LinhaDao.listAsVector();
        this.setGridData(data);
    }

    @Override
    public void reloadDefaults() {
        this.getFieldNumero().setEditable(false);
    }

    public void setGridData(Object[][] data) {
        DefaultTableModel model = (DefaultTableModel) grid.getModel();
        model.setDataVector(data, new String [] {
            "Número", "Cliente", "Plano", "Ativação", "Cancelamento", "Dia de pagamento"
        });
    }

    private void initGrid(){
        try {
            /**
             * Define os dados do grid
             */
            updateGridData();

            /**
             * Listener para a seleção de linhas no grid - captura valores e define-os no formulário
             */
            grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    if(grid.getModel().getRowCount() > grid.getSelectedRow() && grid.getSelectedRow() != -1){
                        Object numero = grid.getModel().getValueAt(grid.getSelectedRow(), 0);
                        Object cliente = grid.getModel().getValueAt(grid.getSelectedRow(), 1);
                        Object plano = grid.getModel().getValueAt(grid.getSelectedRow(), 2);
                        Object ativacao = grid.getModel().getValueAt(grid.getSelectedRow(), 3);
                        Object cancelamento = grid.getModel().getValueAt(grid.getSelectedRow(), 4);
                        Object pagamento = grid.getModel().getValueAt(grid.getSelectedRow(), 5);
                        fieldNumero.setEditable(false);
                        fieldNumero.setText(numero.toString());
                        fieldCliente.setText(cliente.toString());
                        fieldPlano.setText(plano.toString());
                        fieldAtivacao.setText(ativacao.toString());
                        
                        if(cancelamento != null)
                            fieldCancelamento.setText(cancelamento.toString());
                        else
                            fieldCancelamento.setText("");
                        
                        fieldPagamento.setText(pagamento.toString());
                    }
                }
            });
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabs = new javax.swing.JTabbedPane();
        tabGrid = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grid = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        buttonNovo = new javax.swing.JButton();
        buttonEditar = new javax.swing.JButton();
        buttonApagar = new javax.swing.JButton();
        buttonBuscar = new javax.swing.JButton();
        buttonConta = new javax.swing.JButton();
        tabForm = new javax.swing.JPanel();
        labelNumero = new javax.swing.JLabel();
        labelCliente = new javax.swing.JLabel();
        labelPlano = new javax.swing.JLabel();
        buttonSalvar = new javax.swing.JButton();
        fieldCliente = new javax.swing.JTextField();
        fieldPlano = new javax.swing.JTextField();
        fieldNumero = new javax.swing.JTextField();
        fieldPagamento = new javax.swing.JTextField();
        labelValorMensal = new javax.swing.JLabel();
        labelValorChamada = new javax.swing.JLabel();
        labelPagamento = new javax.swing.JLabel();
        fieldCancelamento = new javax.swing.JFormattedTextField();
        fieldAtivacao = new javax.swing.JFormattedTextField();

        setPreferredSize(new java.awt.Dimension(971, 447));

        grid.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Número", "Cliente", "Plano", "Ativação", "Cancelamento", "Dia de Pagamento"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(grid);

        jToolBar1.setRollover(true);

        buttonNovo.setText("Novo");
        buttonNovo.setFocusable(false);
        buttonNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonNovo);

        buttonEditar.setText("Editar");
        buttonEditar.setFocusable(false);
        buttonEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonEditar);

        buttonApagar.setText("Apagar");
        buttonApagar.setFocusable(false);
        buttonApagar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonApagar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonApagar);

        buttonBuscar.setText("Buscar");
        buttonBuscar.setFocusable(false);
        buttonBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonBuscar);

        buttonConta.setText("Conta Telefônica");
        buttonConta.setFocusable(false);
        buttonConta.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonConta.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonConta);

        javax.swing.GroupLayout tabGridLayout = new javax.swing.GroupLayout(tabGrid);
        tabGrid.setLayout(tabGridLayout);
        tabGridLayout.setHorizontalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabGridLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabGridLayout.setVerticalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabGridLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabs.addTab("Dados", tabGrid);

        labelNumero.setText("Número");

        labelCliente.setText("Cliente");

        labelPlano.setText("Plano");

        buttonSalvar.setText("Salvar");

        fieldPagamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldPagamentoActionPerformed(evt);
            }
        });

        labelValorMensal.setText("Cancelamento");

        labelValorChamada.setText("Ativação");

        labelPagamento.setText("Dia de pagamento");

        try {
            fieldCancelamento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-##-## ##:##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            fieldAtivacao.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-##-## ##:##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout tabFormLayout = new javax.swing.GroupLayout(tabForm);
        tabForm.setLayout(tabFormLayout);
        tabFormLayout.setHorizontalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelPagamento)
                    .addComponent(labelPlano)
                    .addComponent(labelNumero)
                    .addComponent(labelCliente)
                    .addComponent(labelValorChamada)
                    .addComponent(labelValorMensal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(fieldCancelamento)
                    .addComponent(fieldAtivacao)
                    .addComponent(buttonSalvar)
                    .addComponent(fieldNumero)
                    .addComponent(fieldCliente)
                    .addComponent(fieldPlano, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                    .addComponent(fieldPagamento, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE))
                .addGap(107, 107, 107))
        );
        tabFormLayout.setVerticalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNumero)
                    .addComponent(fieldNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPlano)
                    .addComponent(fieldPlano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelValorChamada)
                    .addComponent(fieldAtivacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelValorMensal)
                    .addComponent(fieldCancelamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPagamento)
                    .addComponent(fieldPagamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(buttonSalvar)
                .addContainerGap())
        );

        tabs.addTab("Formulário", tabForm);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 694, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(242, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(43, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void fieldPagamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldPagamentoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_fieldPagamentoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonApagar;
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JButton buttonConta;
    private javax.swing.JButton buttonEditar;
    private javax.swing.JButton buttonNovo;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JFormattedTextField fieldAtivacao;
    private javax.swing.JFormattedTextField fieldCancelamento;
    private javax.swing.JTextField fieldCliente;
    private javax.swing.JTextField fieldNumero;
    private javax.swing.JTextField fieldPagamento;
    private javax.swing.JTextField fieldPlano;
    private javax.swing.JTable grid;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel labelCliente;
    private javax.swing.JLabel labelNumero;
    private javax.swing.JLabel labelPagamento;
    private javax.swing.JLabel labelPlano;
    private javax.swing.JLabel labelValorChamada;
    private javax.swing.JLabel labelValorMensal;
    private javax.swing.JPanel tabForm;
    private javax.swing.JPanel tabGrid;
    private javax.swing.JTabbedPane tabs;
    // End of variables declaration//GEN-END:variables

    public JButton getButtonApagar() {
        return buttonApagar;
    }

    public void setButtonApagar(JButton buttonApagar) {
        this.buttonApagar = buttonApagar;
    }

    public JButton getButtonBuscar() {
        return buttonBuscar;
    }

    public void setButtonBuscar(JButton buttonBuscar) {
        this.buttonBuscar = buttonBuscar;
    }

    public JButton getButtonEditar() {
        return buttonEditar;
    }

    public void setButtonEditar(JButton buttonEditar) {
        this.buttonEditar = buttonEditar;
    }

    public JButton getButtonNovo() {
        return buttonNovo;
    }

    public void setButtonNovo(JButton buttonNovo) {
        this.buttonNovo = buttonNovo;
    }

    public JButton getButtonSalvar() {
        return buttonSalvar;
    }

    public void setButtonSalvar(JButton buttonSalvar) {
        this.buttonSalvar = buttonSalvar;
    }

    public JFormattedTextField getFieldAtivacao() {
        return fieldAtivacao;
    }

    public void setFieldAtivacao(JFormattedTextField fieldAtivacao) {
        this.fieldAtivacao = fieldAtivacao;
    }

    public JFormattedTextField getFieldCancelamento() {
        return fieldCancelamento;
    }

    public void setFieldCancelamento(JFormattedTextField fieldCancelamento) {
        this.fieldCancelamento = fieldCancelamento;
    }

    public JTextField getFieldCliente() {
        return fieldCliente;
    }

    public void setFieldCliente(JTextField fieldCliente) {
        this.fieldCliente = fieldCliente;
    }

    public JTextField getFieldNumero() {
        return fieldNumero;
    }

    public void setFieldNumero(JTextField fieldNumero) {
        this.fieldNumero = fieldNumero;
    }

    public JTextField getFieldPagamento() {
        return fieldPagamento;
    }

    public void setFieldPagamento(JTextField fieldPagamento) {
        this.fieldPagamento = fieldPagamento;
    }

    public JTextField getFieldPlano() {
        return fieldPlano;
    }

    public void setFieldPlano(JTextField fieldPlano) {
        this.fieldPlano = fieldPlano;
    }

    public JTable getGrid() {
        return grid;
    }

    public void setGrid(JTable grid) {
        this.grid = grid;
    }

    public JLabel getLabelCliente() {
        return labelCliente;
    }

    public void setLabelCliente(JLabel labelCliente) {
        this.labelCliente = labelCliente;
    }

    public JLabel getLabelNumero() {
        return labelNumero;
    }

    public void setLabelNumero(JLabel labelNumero) {
        this.labelNumero = labelNumero;
    }

    public JLabel getLabelPagamento() {
        return labelPagamento;
    }

    public void setLabelPagamento(JLabel labelPagamento) {
        this.labelPagamento = labelPagamento;
    }

    public JLabel getLabelPlano() {
        return labelPlano;
    }

    public void setLabelPlano(JLabel labelPlano) {
        this.labelPlano = labelPlano;
    }

    public JLabel getLabelValorChamada() {
        return labelValorChamada;
    }

    public void setLabelValorChamada(JLabel labelValorChamada) {
        this.labelValorChamada = labelValorChamada;
    }

    public JLabel getLabelValorMensal() {
        return labelValorMensal;
    }

    public void setLabelValorMensal(JLabel labelValorMensal) {
        this.labelValorMensal = labelValorMensal;
    }

    public JPanel getTabForm() {
        return tabForm;
    }

    public void setTabForm(JPanel tabForm) {
        this.tabForm = tabForm;
    }

    public JPanel getTabGrid() {
        return tabGrid;
    }

    public void setTabGrid(JPanel tabGrid) {
        this.tabGrid = tabGrid;
    }

    public JTabbedPane getTabs() {
        return tabs;
    }

    public void setTabs(JTabbedPane tabs) {
        this.tabs = tabs;
    }
    
}
